package com.gsn.dataCommon;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.gsn.name.Sort;

public class ConfigModel {
	public static ChannelModel channelModelList[];
	public static ConfigModel instance;
	public static ArrayList<TableModel> tableModelList = new ArrayList<TableModel>();

	public static GiftModel giftModelList[];
	public static ShopModel shopModelList[];
	public static int leafUser[];
	public static int selectedLeaf;
	public static long jackpot;
	public static int maxPerChannel;
	public static int selectedChannel;
	public static int sort = Sort.NUM_MAX_MIN;
	public static int beanNeedSupport;
	public static String version = "1.0.1";
	public static JSONObject portalJson;

	public ConfigModel() {
		instance = this;
		leafUser = new int[100];
	}

	public static ConfigModel getInstance() {
		if (instance == null) {
			return new ConfigModel();
		}

		return instance;
	}

	public static void deleteRoom(int id) {
		// TODO Auto-generated method stub
		for (int i = 0; i < tableModelList.size(); i++) {
			if (tableModelList.get(i).id == id) {
				tableModelList.remove(i);
				break;
			}
		}

	}

	public static void sort() {

		switch (sort) {
		case Sort.INDEX_MAX_MIN:
			sortNumMaxmin();
			break;
		case Sort.INDEX_MIN_MAX:
			sortNumMinMax();
			break;
		case Sort.NUM_MAX_MIN:
			sortNumPeopleMaxmin();
			break;
		case Sort.NUM_MIN_MAX:
			sortNumPeopleMinMax();
			break;
		case Sort.BET_MAX_MIN:
			sortBetMaxmin();
			break;
		case Sort.BET_MIN_MAX:
			sortBetMinMax();
			break;

		default:
			break;
		}

	}

	public static void sortNumPeopleMaxmin() {
		int i, j;
		TableModel t1, t2, t3;

		t3 = new TableModel();
		for (i = 0; i < tableModelList.size() - 1; i++) {
			t1 = tableModelList.get(i);
			for (j = i + 1; j < tableModelList.size(); j++) {
				t2 = tableModelList.get(j);
				if ((t1.personCount == 5 || t1.personCount < t2.personCount) && t2.personCount != 5) {

					t3.copy(t1);
					tableModelList.get(i).copy(t2);
					tableModelList.get(j).copy(t3);
				}
			}
		}
	}

	public static void sortNumPeopleMinMax() {
		int i, j;
		TableModel t1, t2, t3;
		t3 = new TableModel();
		for (i = 0; i < tableModelList.size() - 1; i++) {
			t1 = tableModelList.get(i);
			for (j = i + 1; j < tableModelList.size(); j++) {
				t2 = tableModelList.get(j);
				if (t1.personCount > t2.personCount) {
					t3.copy(t1);
					tableModelList.get(i).copy(t2);
					tableModelList.get(j).copy(t3);
				}
			}
		}
		for (i = 0; i < tableModelList.size(); i++)
			System.out.print(tableModelList.get(i).personCount + " ");
		System.out.println();
	}

	public static void sortNumMaxmin() {
		int i, j;
		TableModel t1, t2, t3;

		t3 = new TableModel();
		for (i = 0; i < tableModelList.size() - 1; i++) {
			t1 = tableModelList.get(i);
			for (j = i + 1; j < tableModelList.size(); j++) {
				t2 = tableModelList.get(j);
				if (t1.index < t2.index) {

					t3.copy(t1);
					tableModelList.get(i).copy(t2);
					tableModelList.get(j).copy(t3);
				}
			}
		}
	}

	public static void sortNumMinMax() {
		int i, j;
		TableModel t1, t2, t3;

		t3 = new TableModel();
		for (i = 0; i < tableModelList.size() - 1; i++) {
			t1 = tableModelList.get(i);
			for (j = i + 1; j < tableModelList.size(); j++) {
				t2 = tableModelList.get(j);
				if (t1.index > t2.index) {

					t3.copy(t1);
					tableModelList.get(i).copy(t2);
					tableModelList.get(j).copy(t3);
				}
			}
		}
	}

	public static void sortBetMaxmin() {
		int i, j;
		TableModel t1, t2, t3;

		t3 = new TableModel();
		for (i = 0; i < tableModelList.size() - 1; i++) {
			t1 = tableModelList.get(i);
			for (j = i + 1; j < tableModelList.size(); j++) {
				t2 = tableModelList.get(j);
				if (t1.bet < t2.bet) {

					t3.copy(t1);
					tableModelList.get(i).copy(t2);
					tableModelList.get(j).copy(t3);
				}
			}
		}
	}

	public static void sortBetMinMax() {
		int i, j;
		TableModel t1, t2, t3;

		t3 = new TableModel();
		for (i = 0; i < tableModelList.size() - 1; i++) {
			t1 = tableModelList.get(i);
			for (j = i + 1; j < tableModelList.size(); j++) {
				t2 = tableModelList.get(j);
				if (t1.bet > t2.bet) {

					t3.copy(t1);
					tableModelList.get(i).copy(t2);
					tableModelList.get(j).copy(t3);
				}
			}
		}
	}

	public static void sortNameMaxmin() {
		int i, j;
		TableModel t1, t2, t3;

		t3 = new TableModel();
		for (i = 0; i < tableModelList.size() - 1; i++) {
			t1 = tableModelList.get(i);
			for (j = i + 1; j < tableModelList.size(); j++) {
				t2 = tableModelList.get(j);
				if (t1.tableName.compareTo(t2.tableName) < 0) {

					t3.copy(t1);
					tableModelList.get(i).copy(t2);
					tableModelList.get(j).copy(t3);
				}
			}
		}

	}

	public static void sortNameMinMax() {
		int i, j;
		TableModel t1, t2, t3;

		t3 = new TableModel();
		for (i = 0; i < tableModelList.size() - 1; i++) {
			t1 = tableModelList.get(i);
			for (j = i + 1; j < tableModelList.size(); j++) {
				t2 = tableModelList.get(j);
				if (t1.tableName.compareTo(t2.tableName) < 0) {

					t3.copy(t1);
					tableModelList.get(i).copy(t2);
					tableModelList.get(j).copy(t3);
				}
			}
		}

	}

	public static void updateTable(JSONArray tableId, JSONArray personCount, JSONArray totalCount) {
		int i, j;

		int id;
		TableModel table;
		try {
			for (i = 0; i < tableId.length(); i++) {
				id = tableId.getInt(i);
				for (j = 0; j < tableModelList.size(); j++) {
					table = tableModelList.get(j);
					if (table.id == id) {
						table.personCount = personCount.getInt(i);
						table.totalCount = totalCount.getInt(i);
						break;
					}
				}
			}

			if (sort == Sort.INDEX_MAX_MIN || sort == Sort.INDEX_MIN_MAX) {
				sort();
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}

}
